package com.scalar.chiptrack.adaptor.beceem;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.FabSortLot;
import com.scalar.chiptrack.adaptor.spil.SPILAdaptor;
import com.scalar.chiptrack.adaptor.utils.ParserResult;
import com.scalar.chiptrack.adaptor.utils.Parser;
import com.scalar.chiptrack.adaptor.utils.ParserFactory;
import com.scalar.chiptrack.adaptor.utils.XLSParserImpl;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.NewAdaptorDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.process.ParameterTypes;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 13 Oct, 2010
 * Time: 5:10:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class SPILDataParser extends JobHandler implements AdaptorConstants
{
    private HashMap<String, InventoryLot> m_dieBankDataMap  = new HashMap<String, InventoryLot>();
    private HashMap<String, AssemblyLot> m_assyDataMap      = new HashMap<String, AssemblyLot>();
    private HashMap<String, TestLot> m_ftDataMap            = new HashMap<String, TestLot>();
    private HashMap<String, InventoryLot> m_fgDataMap         = new HashMap<String, InventoryLot>();

    private Logger m_parserLogger       = null;
    private String m_dataOutputDir      = null;

    public SPILDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir(VENDOR_NAME_SPIL, WIP_TYPE_ASSEMBLY);
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );

            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_SPIL );
            AssemblyAdaptorDAO.setLogger( m_parserLogger );
            TestAdaptorDAO.setLogger( m_parserLogger );
            InventoryLotDAO.setLogger( m_parserLogger );
            AdaptorUtils.setLogger(m_parserLogger);
        }
        catch (ScalarException se)
        {
            log( "ERROR!! ERROR!! ERROR!! Exception while loading/creating SPIL output directory from database, Exception: " + se );
            throw se;
        }
    }
    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main( String args[] ) throws Exception
    {
        try
        {
            new SPILDataParser().execute();
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    /**
     *
     * @throws Exception
     */
    public void execute() throws Exception
    {
        AdaptorUtils.printHeader(VENDOR_NAME_SPIL, CONSTANT_PARSER);

        File doneDestDir = new File( m_dataOutputDir + "/done" );

        File sourceDataoutDir = new File( m_dataOutputDir );

        File[] fileList = sourceDataoutDir.listFiles();
        if ( ( fileList == null ) || ( fileList.length <= 3 ) )
        {
            log( "No file found in directory: " + sourceDataoutDir );
            throw new ScalarException( "\n+-----------No file found in directory " + sourceDataoutDir );
        }

        ArrayList<File> successFileList = new ArrayList<File>();

        boolean success = false;

        for ( int i = 0; i < fileList.length; i++ )
        {
            if ( fileList[i].isDirectory() == true )
            {
                continue;
            }

            try
            {
                // Step 1
                // Parse Files to get Object List
                if ( fileList[i].getName() != null &&  fileList[i].getName().startsWith( SPILAdaptor.FILE_NAME_WIP_STATUS ) )
                {
                    // To parse the the SPIL_WIP_STATUS sheet in to table
                    parseSpilWipReport( fileList[i].getAbsolutePath()  );

                    successFileList.add( fileList[i] );
                }
                success = true;
            }
            catch ( Exception e )
            {
                success = false;
                log( "Exception while parsing and storing file: " + fileList[i].getAbsolutePath()  + "\nException: " + e );
                e.printStackTrace();
                throw e;
            }
        }

        try
        {
            // Save DIE BANK Information
            if ( m_dieBankDataMap.isEmpty() == false )
            {
                log( " Updating SPIL Die bank Lot Information" );
                InventoryLotDAO.saveInventoryLot( null, m_dieBankDataMap.values().iterator() , LOCATION_TYPE_DIE_BANK, VENDOR_NAME_SPIL );
            }
            else
            {
                InventoryLotDAO.closeInventoryLot( null, LOCATION_TYPE_DIE_BANK, VENDOR_NAME_SPIL );
            }
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update DIE BANK Lot information into database: " + se );
            throw se;
        }

        try
        {
            // Save Assembly Information
            if ( m_assyDataMap.isEmpty() == false )
            {
                log( " Updating Spil Assembly Lot Information" );
                AssemblyAdaptorDAO.saveAssemblyLot( null, m_assyDataMap.values().iterator(), WIP_TYPE_ASSEMBLY, VENDOR_NAME_SPIL );
            }
            else
            {
                AssemblyAdaptorDAO.closeAssemblyLot( null, WIP_TYPE_ASSEMBLY, VENDOR_NAME_SPIL);
            }

            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se );
            throw se;
        }
        // Saving the Test Lot Information
        try
        {
            // Save Final Test Information
            if ( m_ftDataMap.isEmpty() == false )
            {
                log( " Updating Spil China Test FT Lot Information" );
                TestAdaptorDAO.saveTestLot( null,  m_ftDataMap.values().iterator(),LOCATION_TYPE_FT, VENDOR_NAME_SPIL );
            }
            else
            {
                TestAdaptorDAO.closeTestLot(null, LOCATION_TYPE_FT, VENDOR_NAME_SPIL);
            }

            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se );
            throw se;
        }

         // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
        AdaptorDAO.sendAllNotifications(VENDOR_NAME_SPIL, true, true, true);

        // To Close the Drop Out Lots
        AdaptorDAO.closeDropOutLots(VENDOR_NAME_SPIL);

        int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( SPILAdaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

        // Update Adaptor_Run_Info table
        try
        {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, SPILAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, SPILAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
         }
        // Move the successful files to 'done' directory
        if ( success )
        {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
        }
    }
    /**
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private void parseSpilWipReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result = null;
        String fablotNumber     = null;
        String lotNumber        = null;
        String partNumber       = null;
        AssemblyLot assemblyLot = null;
        TestLot testLot         = null;
        InventoryLot inventoryLot = null;
        try
        {
            File wipFile = new File( fileName );
            if ( wipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "Parsing the file for Spil WIP Status Report..." );
            System.out.println("\nFound " + fileName);
            System.out.println("Parsing the file for Spil WIP Status Report...");

            Parser parser = ParserFactory.getXLSParser( wipFile );

            ((XLSParserImpl)parser).setHeaderRowIdx(3);
            ((XLSParserImpl)parser).setDataStartRowIdx(4);

            result = ((XLSParserImpl)parser).parse();

            //System.out.println("result.getHeadings() = " + result.getHeadings());

             while ( result.next() )
             {
                fablotNumber = result.getString("CUST_LOT");
                partNumber = result.getString("DEV");
                lotNumber  = result.getString("PROD_LOT");
                if ( lotNumber == null )
                {
                    lotNumber = fablotNumber;
                }
                if ( partNumber != null && partNumber.trim().length() > 0 && !"SubTotal".equalsIgnoreCase( partNumber )
                        && lotNumber != null && lotNumber.trim().length() >0   )
                {
                    /* Wafer Store Lots*/
                    /*if (wipStatus.getWsbk() != null && !"0".equalsIgnoreCase( wipStatus.getWsbk() )  )
                    {
                        wsInventoryLot = (InventoryLot) m_waferBankDataMap.get(partNumber + fablotNumber);
                        if (wsInventoryLot == null)
                        {
                            wsInventoryLot = new InventoryLot(VENDOR_NAME_SPIL_CHINA, LOCATION_TYPE_WAFER_STORE );
                            m_waferBankDataMap.put(partNumber + fablotNumber, wsInventoryLot);
                        }

                        int quantity = 0;
                        if (wsInventoryLot.getWaferQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt( wsInventoryLot.getWaferQtyIn() );
                            }
                            catch (NumberFormatException npe) {
                            }
                            try
                            {
                                quantity += Integer.parseInt( wipStatus.getWsbk() );
                            }
                            catch (NumberFormatException npe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt(wipStatus.getWsbk());
                            }
                            catch (NumberFormatException npe) {
                            }
                        }

                        if (quantity > 0)
                        {
                            wsInventoryLot.setLotNumber(fablotNumber);
                            wsInventoryLot.setPartNumber(partNumber);
                            wsInventoryLot.setDateIn(wipStatus.getWsrcv());
                            wsInventoryLot.setWaferQtyIn(quantity + "");
                            wsInventoryLot.setStatus(LOT_STATUS_RUNNING);
                            wsInventoryLot.setFabLotNumber(fablotNumber);
                            wsInventoryLot.setCategory(LOT_CATEGORY_TYPE_PROD);
                        }
                    }*/

                    //SORT
                    /*if ( wipStatus.getProbe() != null && !"0".equalsIgnoreCase(wipStatus.getProbe() )
                           || wipStatus.getWsfgWafer() != null && !"0".equalsIgnoreCase(wipStatus.getWsfgWafer() )
                            || wipStatus.getWsTran() != null && !"0".equalsIgnoreCase(wipStatus.getWsTran() ) )
                    {
                        log( lotNumber + "in Sort ");

                        fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fablotNumber );

                        if (fabSortLot == null)
                        {
                            fabSortLot = new FabSortLot(VENDOR_NAME_SPIL_CHINA, WIP_TYPE_SORT );
                            m_sortDataMap.put( partNumber + fablotNumber, fabSortLot);
                        }

                        if (wipStatus.getProbe() != null && !"0".equalsIgnoreCase(wipStatus.getProbe() ) )
                        {
                            fabSortLot = (FabSortLot) m_sortDataMap.get(partNumber + fablotNumber );

                            if( fabSortLot.getQtyIn() != null )
                            {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + Integer.parseInt( wipStatus.getProbe() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( wipStatus.getProbe() );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( wipStatus.getProbe() );
                            }

                            fabSortLot.setStage("Probe");
                        }

                        if ( wipStatus.getWsfgWafer() != null && !"0".equalsIgnoreCase(wipStatus.getWsfgWafer() ) )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fablotNumber );

                            if( fabSortLot.getQtyIn() != null )
                            {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + Integer.parseInt( wipStatus.getWsfgWafer() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( wipStatus.getWsfgWafer() );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( wipStatus.getWsfgWafer() );
                            }

                            fabSortLot.setStage("After Probe");
                        }

                        if ( wipStatus.getWsTran() != null && !"0".equalsIgnoreCase(wipStatus.getWsTran() ) &&  Integer.parseInt( wipStatus.getWsTran() ) <= 25 )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fablotNumber );

                            if( fabSortLot.getQtyIn() != null )
                            {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + Integer.parseInt( wipStatus.getWsTran() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( wipStatus.getWsTran() );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( wipStatus.getWsTran() );
                            }

                            fabSortLot.setStage("InHouse Transit");
                        }

                        fabSortLot.setCustProduct( partNumber );
                        fabSortLot.setLotId( fablotNumber );
                        fabSortLot.setForcastedDateOut( wipStatus.getWsSOD() );
                        fabSortLot.setPkgType( wipStatus.getPackageType() );
                        fabSortLot.setLeadCount( wipStatus.getLeadCount() );
                        fabSortLot.setDateIn( wipStatus.getWsrcv() );
                        fabSortLot.setFabLotNumber( fablotNumber );
                        fabSortLot.setLotStatus( LOT_STATUS_RUNNING );
                        fabSortLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );

                    }*/

                    //Die BAnK
                    if ( result.getInt(16) > 0 )
                    {
                        inventoryLot = (InventoryLot) m_dieBankDataMap.get( partNumber + fablotNumber  );
                        if ( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot(VENDOR_NAME_SPIL, LOCATION_TYPE_DIE_BANK );
                            m_dieBankDataMap.put( partNumber + fablotNumber , inventoryLot );
                        }

                        int quantity = 0;
                        if (inventoryLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(inventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            if ( result.getInt(15) > 0 && result.getInt(15) == result.getInt(16) )
                            {
                                int fabProcessStepId = NewAdaptorDAO.getProcessStepId( partNumber, WIP_TYPE_FAB, WIP_TYPE_FAB );
                                int gdpw = 0;
                                try
                                {
                                    gdpw = Integer.parseInt(NewProductDAO.getProcessParameterValue( null, fabProcessStepId, ParameterTypes.grossDieWafer ) ) ;
                                }
                                catch( NumberFormatException nfe ) { gdpw = 0; }

                                quantity += result.getInt("ACT_QTY")*gdpw;
                            }
                            else
                            {
                                quantity += result.getInt("ACT_QTY");
                            }
                        }
                        else
                        {
                            if ( result.getInt(15) > 0 && result.getInt(15) == result.getInt(16) )
                            {
                                int fabProcessStepId = NewAdaptorDAO.getProcessStepId( partNumber, WIP_TYPE_FAB, WIP_TYPE_FAB );
                                int gdpw = 0;
                                try
                                {
                                    gdpw = Integer.parseInt(NewProductDAO.getProcessParameterValue( null, fabProcessStepId, ParameterTypes.grossDieWafer ) ) ;
                                }
                                catch( NumberFormatException nfe ) { gdpw = 0; }
                                quantity = result.getInt("ACT_QTY") * gdpw;
                            }
                            else
                            {
                                quantity = result.getInt("ACT_QTY");
                            }   
                        }
                        if ( quantity > 0 )
                        {
                            inventoryLot.setLotNumber( fablotNumber );
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setStatus( LOT_STATUS_RUNNING );
                            inventoryLot.setFabLotNumber( fablotNumber );
                            inventoryLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            inventoryLot.setQtyIn( quantity +"" );
                            inventoryLot.setDateIn( StringUtils.formatDateToString( result.getDate(13)) );
                        }
                    }
                    //Assemblt
                    if (  result.getInt("2/O") > 0 || result.getInt("D/A") > 0 || result.getInt("W/B") > 0
                        || result.getInt("MOLD") > 0  || result.getInt("PLAT") > 0 || result.getInt("FORM") > 0
                        || result.getInt(25) > 0 || result.getInt("TRAN") > 0 )
                    {
                         assemblyLot = (AssemblyLot)m_assyDataMap.get( partNumber + lotNumber + fablotNumber );

                         if (assemblyLot == null)
                         {
                             assemblyLot = new AssemblyLot( VENDOR_NAME_SPIL, WIP_TYPE_ASSEMBLY );
                             m_assyDataMap.put( partNumber + lotNumber + fablotNumber, assemblyLot);
                         }

                         if (result.getInt("2/O") > 0 )
                         {
                             assemblyLot = (AssemblyLot)m_assyDataMap.get( partNumber + lotNumber + fablotNumber );

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("2/O") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("2/O")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("2/O")+"");
                            }

                            if (assemblyLot.getStage() != null && assemblyLot.getStage().indexOf("2nd Optical") == -1 )
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", 2nd Optical");
                            }
                            else
                            {
                                assemblyLot.setStage("2nd Optical");
                            }
                        }
                        if (result.getInt("D/A") > 0 )
                        {
                            assemblyLot = (AssemblyLot)m_assyDataMap.get( partNumber + lotNumber + fablotNumber );

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("D/A") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("D/A")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("D/A")+"");
                            }

                            if (assemblyLot.getStage() != null && assemblyLot.getStage().indexOf("Die Attach")== -1 )
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Die Attach");
                            }
                            else
                            {
                                assemblyLot.setStage("Die Attach");
                            }
                        }

                        if (result.getInt("W/B") > 0 )
                        {
                            assemblyLot = (AssemblyLot)m_assyDataMap.get( partNumber + lotNumber + fablotNumber );

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("W/B") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("W/B") + "");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("W/B")+"");
                            }

                            if (assemblyLot.getStage() != null && assemblyLot.getStage().indexOf("Wire Bond") == -1 )
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Wire Bond");
                            }
                            else
                            {
                                assemblyLot.setStage("Wire Bond");
                            }
                        }
                        if (  result.getInt("MOLD") > 0   )
                        {
                            assemblyLot = (AssemblyLot)m_assyDataMap.get( partNumber + lotNumber + fablotNumber );

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("MOLD") +"") + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("MOLD")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("MOLD")+"");
                            }

                            if (assemblyLot.getStage() != null && assemblyLot.getStage().indexOf("Molding") == -1 )
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Molding");
                            }
                            else
                            {
                                assemblyLot.setStage("Molding");
                            }
                        }

                        if (result.getInt("PLAT") > 0 )
                        {
                            assemblyLot = (AssemblyLot)m_assyDataMap.get( partNumber + lotNumber + fablotNumber );

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("PLAT")) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("PLAT")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("PLAT") +"");
                            }

                            if (assemblyLot.getStage() != null && assemblyLot.getStage().indexOf("Plating") == -1 )
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Plating");
                            }
                            else
                            {
                                assemblyLot.setStage("Plating");
                            }
                        }
                        if (result.getInt("FORM") > 0 )
                        {
                            assemblyLot = (AssemblyLot)m_assyDataMap.get( partNumber + lotNumber + fablotNumber );

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("FORM") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("FORM") +"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("FORM")+"");
                            }

                            if (assemblyLot.getStage() != null && assemblyLot.getStage().indexOf("Forming") == -1 )
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Forming");
                            }
                            else
                            {
                                assemblyLot.setStage("Forming");
                            }
                        }

                        if (result.getInt(25) > 0 )
                        {
                            assemblyLot = (AssemblyLot)m_assyDataMap.get( partNumber + lotNumber + fablotNumber );

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt(25) ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt(25)+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt(25)+"");
                            }

                            if (assemblyLot.getStage() != null && assemblyLot.getStage().indexOf("Packing") == 1 )
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Packing");
                            }
                            else
                            {
                                assemblyLot.setStage("Packing");
                            }
                        }
                        if (result.getInt("TRAN") > 0 )
                        {
                            assemblyLot = (AssemblyLot)m_assyDataMap.get( partNumber + lotNumber + fablotNumber );

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("TRAN") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("TRAN") +"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("TRAN")+"");
                            }

                            if (assemblyLot.getStage() != null && assemblyLot.getStage().indexOf("Transit") == -1 )
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Transit");
                            }
                            else
                            {
                                assemblyLot.setStage("Transit");
                            }
                        }
                        if ( assemblyLot.getQtyIn() != null && !"0".equalsIgnoreCase( assemblyLot.getQtyIn() ))
                        {
                            assemblyLot.setDeviceName(partNumber);
                            assemblyLot.setLotNumber(lotNumber);
                            assemblyLot.setFabLotNumber(fablotNumber);
                            assemblyLot.setStatus(LOT_STATUS_RUNNING);
                            assemblyLot.setPackageType(result.getString("PKG"));
                            assemblyLot.setLeadCount( result.getString("L/C"));
                            assemblyLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            if ( result.getDate("LOAD") != null )
                            {
                                assemblyLot.setDateIn( StringUtils.formatDateToString(result.getDate("LOAD") )  );
                            }
                            else if ( result.getDate(15) != null )
                            {
                                assemblyLot.setDateIn( StringUtils.formatDateToString( result.getDate(15)));
                            }

                            if ( result.getDate(26) != null  )
                            {
                                assemblyLot.setExpectedDateOut( StringUtils.formatDateToString( result.getDate(26)) );
                            }

                         }
                   }//END OF ASSY

                   //TEST WIP
                    if ( result.getInt("FTBK") > 0 || result.getInt("F/T") > 0 || result.getInt("L/S") > 0 || result.getInt("BAKE") > 0
                          || result.getInt(34) > 0  )
                    {
                        testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                        if (testLot == null)
                        {
                            testLot = new TestLot(VENDOR_NAME_SPIL);
                            m_ftDataMap.put(partNumber + lotNumber + fablotNumber, testLot);
                        }

                       // FOR SETTING THE QTY IN
                       if( result.getInt("FTBK") > 0  )
                       {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );

                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("FTBK") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("FTBK")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("FTBK") + "" );
                            }
                            if ( testLot.getStage() != null && testLot.getStage().indexOf("FTBK") == -1 )
                            {
                                testLot.setStage( testLot.getStage() + ", FTBK");
                            }
                            else
                            {
                                testLot.setStage("FTBK");
                            }
                        }

                        if( result.getInt("F/T") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("F/T") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("F/T")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("F/T") + "" );
                            }
                            if ( testLot.getStage() != null && testLot.getStage().indexOf("F/T") == -1 )
                            {
                                testLot.setStage( testLot.getStage() + ", F/T");
                            }
                            else
                            {
                                testLot.setStage("F/T");
                            }
                        }
                        if( result.getInt("L/S") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("L/S") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("L/S")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("L/S") + "" );
                            }
                            if ( testLot.getStage() != null && testLot.getStage().indexOf("L/S") == -1 )
                            {
                                testLot.setStage( testLot.getStage() + ", L/S");
                            }
                            else
                            {
                                testLot.setStage("L/S");
                            }
                        }
                        if( result.getInt("BAKE") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("BAKE") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("BAKE")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("BAKE") + "" );
                            }
                            if ( testLot.getStage() != null && testLot.getStage().indexOf("BAKE") == -1 )
                            {
                                testLot.setStage( testLot.getStage() + ", BAKE");
                            }
                            else
                            {
                                testLot.setStage("BAKE");
                            }
                        }
                        if( result.getInt(34) > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt(34) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt(34)+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt(34) + "" );
                            }
                            if ( testLot.getStage() != null && testLot.getStage().indexOf("Packing") == -1 )
                            {
                                testLot.setStage( testLot.getStage() + ", Packing");
                            }
                            else
                            {
                                testLot.setStage("Packing");
                            }
                        }

                        if ( testLot.getQtyIn() != null && !"0".equalsIgnoreCase(testLot.getQtyIn() ) )
                        {
                            testLot.setPartNumber( partNumber );
                            testLot.setLotNumber( lotNumber );
                            testLot.setFabLotNumber( fablotNumber );
                            if ( result.getDate(29) != null  )
                            {
                                testLot.setDateIn( StringUtils.formatDateToString(result.getDate(29) )  );
                            }

                            if (result.getDate(35) != null )
                            {
                                testLot.setExpecetedDate( StringUtils.formatDateToString(result.getDate(29) )  );
                            }

                            testLot.setLeadCount( result.getString("L/C") );
                            testLot.setPackageType( result.getString("PKG") );
                            testLot.setStatus(LOT_STATUS_RUNNING);
                            testLot.setPsType( LOCATION_TYPE_FT );
                            testLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                        }
                    }//end of TEST WIP
                }
             }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("Exception while parsing the SPIL Wip File" + e);
            throw  e;
        }
    }

    private void log( String message )
    {
        if ( message == null ) return;

        if ( m_parserLogger != null )
        {
            m_parserLogger.logDebug( message + LINE_SEPARATOR );
        }
        else
        {
            System.out.println( message );
        }
    } // end of log method
}


